package com.th.supcom.pds.impl.service.internal;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.vo.PdsPrescDetailVO;
import com.th.supcom.pds.domain.vo.PdsPrescParametersVO;
import com.th.supcom.pds.domain.vo.PdsPrescVO;
import com.th.supcom.pds.domain.vo.PhamInfo;
import com.th.supcom.pds.domain.vo.PrescDecoratorVO;
import com.th.supcom.pds.impl.manager.PdsPrescDetailManager;
import com.th.supcom.pds.impl.manager.PdsPrescManager;
import com.th.supcom.pds.impl.manager.PhamInfoManager;
import com.th.supcom.pds.internal.api.PrescDispensingService;

/**
 * 处方详情表服务
 * 
 * @author LiuQiang
 */
public class PrescDispensingServiceImpl implements PrescDispensingService
{
    private PdsPrescDetailManager m_pdsPrescDetailManager;
    private PdsPrescManager m_pdsPrescManager;
    private PhamInfoManager m_phamInfoManager;
    /** 库存不足 */
    private static final String STOCK_SHORT = "1";
    /** 需要审核 */
    private static final String NEED_QUALIFY = "2";
    /** 已经收费未出库 */
    private static final String CHARGE_OUTSTOCK = "3";
    /** 已收费已出库 */
    private static final String CHARGE_UNOUTSTOCK = "4";

    public void setPhamInfoManager (PhamInfoManager phamInfoManager)
    {

        m_phamInfoManager = phamInfoManager;
    }

    @Override
    public List <PrescDecoratorVO> showPrescInfo (PdsPrescParametersVO pdsPrescParametersVO)
    {

        List <PrescDecoratorVO> prescDecoratorVOs = new ArrayList <PrescDecoratorVO> ();

        List <PdsPrescDetailVO> pdsPrescDetailVOs = m_pdsPrescDetailManager.queryPrescDetail (pdsPrescParametersVO);
        List <PdsPrescVO> pdsPrescVOs = m_pdsPrescManager.queryPresc (pdsPrescParametersVO);
        // 根据处方号或Pvisitid查询药品信息
        List <PhamInfo> list = m_phamInfoManager.queryPhamInfoByCondition (pdsPrescParametersVO);
        for (PdsPrescVO pdsPrescVO : pdsPrescVOs)
        {
            PrescDecoratorVO prescDecoratorVO = new PrescDecoratorVO ();
            prescDecoratorVO.setPdsPrescVO (pdsPrescVO);
            prescDecoratorVO.setPhamInfoList (list);
            List <PdsPrescDetailVO> tmpPrecsDetailVO = new ArrayList <PdsPrescDetailVO> ();
            for (PdsPrescDetailVO pdsPrescDetailVO : pdsPrescDetailVOs)
            {
                if (pdsPrescDetailVO.getPrescNo ().equals (pdsPrescVO.getPrescNo ()))
                {
                    tmpPrecsDetailVO.add (pdsPrescDetailVO);
                }
            }
            pdsPrescDetailVOs.removeAll (tmpPrecsDetailVO);
            prescDecoratorVO.setPdsPrescDetailVOs (tmpPrecsDetailVO);
            prescDecoratorVOs.add (prescDecoratorVO);
        }
        // 检查库存
        prescDecoratorVOs = check (prescDecoratorVOs, new CheckConfig ()
        {

            @Override
            public void matchFlag (PdsPrescVO pdsPrescVO, PdsPrescDetailVO pdsPrescDetailVO,
                                   List <PhamInfo> phamInfolist)
            {

                for (PhamInfo phamInfo2 : phamInfolist)
                {
                    String performedByString = pdsPrescDetailVO.getPdperformedBy () == null ? pdsPrescVO.getPperformedBy ()
                                                                                           : pdsPrescDetailVO.getPdperformedBy ();
                    // 执行科室和编码匹配
                    if (performedByString.equals (phamInfo2.getOrgId ())
                        && pdsPrescDetailVO.getPhamCode ().equals (phamInfo2.getPhamStdCode ()))
                    {

                        // 打库存标记
                        if (phamInfo2.getQuantity ().intValue () < pdsPrescDetailVO.getQuantity ().intValue ())
                        {
                            pdsPrescVO.setFlag (STOCK_SHORT);
                            pdsPrescDetailVO.setFlag (STOCK_SHORT);
                        }
                        break;

                    }
                }

            }

        });
        // 审核检查
        prescDecoratorVOs = check (prescDecoratorVOs, new CheckConfig ()
        {

            @Override
            public void matchFlag (PdsPrescVO pdsPrescVO, PdsPrescDetailVO pdsPrescDetailVO, List <PhamInfo> phamInfo)
            {
                // 审核检查
                if (pdsPrescVO.getControlType () != null && pdsPrescVO.getControlType ().intValue () != 0
                    && pdsPrescVO.getQualifiedIndicator () == null)
                {
                    pdsPrescVO.setFlag (NEED_QUALIFY);
                    pdsPrescDetailVO.setFlag (NEED_QUALIFY);
                }
            }

        });
        // 收费检查
        prescDecoratorVOs = check (prescDecoratorVOs, new CheckConfig ()
        {

            @Override
            public void matchFlag (PdsPrescVO pdsPrescVO, PdsPrescDetailVO pdsPrescDetailVO, List <PhamInfo> phamInfo)
            {
                if (pdsPrescVO.getChargeIndicator () != null && pdsPrescVO.getChargeIndicator ().intValue () == 1)
                {

                    // 未出库
                    if (pdsPrescDetailVO.getEachNumber () == null)
                    {
                        pdsPrescVO.setFlag (CHARGE_UNOUTSTOCK);
                        pdsPrescDetailVO.setFlag (CHARGE_UNOUTSTOCK);
                    }
                    else
                    {
                        pdsPrescVO.setFlag (CHARGE_OUTSTOCK);
                        pdsPrescDetailVO.setFlag (CHARGE_OUTSTOCK);
                    }

                }
            }

        });
        System.out.println ("成功");
        return prescDecoratorVOs;
    }

    /**
     * 检查药品
     * 
     * @param decoratorVOs
     * @param checkConfig
     * @return add by Zli
     */
    private List <PrescDecoratorVO> check (List <PrescDecoratorVO> decoratorVOs, CheckConfig checkConfig)
    {
        for (PrescDecoratorVO prescDecoratorVO : decoratorVOs)
        {
            PdsPrescVO pdsPrescVO = prescDecoratorVO.getPdsPrescVO ();
            List <PdsPrescDetailVO> pdsPrescDetailVOs = prescDecoratorVO.getPdsPrescDetailVOs ();
            List <PhamInfo> phamInfolist = prescDecoratorVO.getPhamInfoList ();
            // 药品明细匹配药品库存
            for (PdsPrescDetailVO pdsPrescDetailVO : pdsPrescDetailVOs)
            {
                checkConfig.matchFlag (pdsPrescVO, pdsPrescDetailVO, phamInfolist);
            }

        }
        return decoratorVOs;
    };

    public void setPdsPrescManager (PdsPrescManager pdsPrescManager)
    {
        m_pdsPrescManager = pdsPrescManager;
    }

    public void setPdsPrescDetailManager (PdsPrescDetailManager pdsPrescDetailManager)
    {
        m_pdsPrescDetailManager = pdsPrescDetailManager;
    }

    @Override
    public String toString ()
    {
        String result = "AuditPrescripServiceImpl [m_pdsPrescDetailManager=" + m_pdsPrescDetailManager + "]";
        return result;
    }

    public interface CheckConfig
    {
        public void matchFlag (PdsPrescVO pdsPrescVO, PdsPrescDetailVO pdsPrescDetailVO, List <PhamInfo> phamInfo);
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PrescDispensingServiceImpl.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}
